@file:OptIn(ExperimentalMaterial3Api::class)

package com.learn.a05_2_mars_photos.ui

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import com.learn.a05_2_mars_photos.ui.screens.HomeScreen
import com.learn.a05_2_mars_photos.ui.screens.MarsViewModel
import com.learn.a05_2_mars_photos.R

/**
 * 应用主入口文件 - 负责构建整体UI框架
 *
 * 使用 Material3 组件搭建应用基础结构，集成视图模型与界面组件
 */
@Composable
fun MarsPhotosApp() {
    /** 创建顶部导航栏的滚动行为控制器 */
    val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
    /** 构建基础脚手架布局 */
    Scaffold(
        modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
        topBar = { MarsTopAppBar(scrollBehavior = scrollBehavior) }
    ) { innerPadding ->
        // 主内容区域
        Surface(modifier = Modifier.fillMaxSize()) {
            // 初始化 ViewModel（通过工厂模式创建）
            val marsViewModel: MarsViewModel = viewModel(factory = MarsViewModel.Factory)

            // 加载主屏幕组件
            HomeScreen(
                marsUiState = marsViewModel.marsUiState,
                retryAction = marsViewModel::getMarsPhotos,
                contentPadding = innerPadding
            )
        }
    }
}

/**
 * 自定义顶部应用栏组件
 * @param scrollBehavior 滚动行为控制器（用于联动内容区域滚动效果）
 * @param modifier 样式修饰符
 */
@Composable
fun MarsTopAppBar(
    scrollBehavior: TopAppBarScrollBehavior,
    modifier: Modifier = Modifier
) {
    CenterAlignedTopAppBar(
        scrollBehavior = scrollBehavior,
        title = {
            Text(
                text = stringResource(R.string.app_name),
                style = MaterialTheme.typography.headlineSmall,
            )
        },
        modifier = modifier
    )
}
